package com.community.system.controller;


import com.community.common.annotation.Log;
import com.community.common.config.minio.MinioConfig;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.page.TableDataInfo;
import com.community.common.enums.BusinessType;
import com.community.common.utils.minio.MinioUtils;
import com.community.common.utils.poi.ExcelUtil;
import com.community.system.domain.Patrol;
import com.community.system.service.IPatrolService;
import io.minio.MinioClient;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/system/patrol")
public class PatrolController extends BaseController {

    @Autowired
    private IPatrolService patrolService;


    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioUtils minioUtils;

    /**
     * 查询巡更表管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patrol patrol) {
        startPage();
        List<Patrol> list = patrolService.selectPatrolList(patrol);
        return getDataTable(list);
    }

    /**
     * 导出巡更表管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:export')")
    @Log(title = "巡更表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patrol patrol) {
        List<Patrol> list = patrolService.selectPatrolList(patrol);
        ExcelUtil<Patrol> util = new ExcelUtil<Patrol>(Patrol.class);
        util.exportExcel(response, list, "巡更表管理数据");
    }

    /**
     * 获取巡更表管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:query')")
    @GetMapping(value = "/{patrolId}")
    public AjaxResult getInfo(@PathVariable("patrolId") Long patrolId) {
        return success(patrolService.selectPatrolByPatrolId(patrolId));
    }

    /**
     * 新增巡更表管理
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:add')")
    @Log(title = "巡更表管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patrol patrol) {
        return toAjax(patrolService.insertPatrol(patrol));
    }

    /**
     * 修改巡更表管理
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:edit')")
    @Log(title = "巡更表管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patrol patrol) {
        return toAjax(patrolService.updatePatrol(patrol));
    }

    /**
     * 删除巡更表管理
     */
    @PreAuthorize("@ss.hasPermi('system:patrol:remove')")
    @Log(title = "巡更表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{patrolIds}")
    public AjaxResult remove(@PathVariable Long[] patrolIds) {
        return toAjax(patrolService.deletePatrolByPatrolIds(patrolIds));
    }



    @PostMapping("/uploadminio")
    public AjaxResult upload(@RequestParam("file") MultipartFile file)// 巡更记录的其他参数（如routeId、communityId等）
    {
        //1.给上传到miniIO的文件起存储的名
        String fileName = UUID.randomUUID().toString();
        //2.上传文件到minio
        String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String newFileName = dir + "/" + fileName;
        minioUtils.upload(file, newFileName);
        //3.获取上传后的文件url
        String url = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + newFileName;

        //4.将url保存到数据库中
        Patrol patrol = new Patrol();
        patrol.setImages(url);
//        patrolService.insertPatrol(patrol);

        //5.将url返回给前端
        return success(url);
    }
}
